<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('阶段')" prop="phase">
            <dict-select
              v-model="form.phase"
              dictionary="fund_phase"
              :placeholder="$t('阶段')"
              @change="phaseChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('期限(年)')" prop="year">
            <el-input v-model="form.year" disabled show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计提基数')" prop="accrualBase">
            <dict-select
              v-model="form.accrualBase"
              dictionary="accrual_base"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.accrualBase === 'ZT'" :span="12">
          <el-form-item :label="$t('在投本金计算方式')" prop="capitalCalType">
            <dict-select
              v-model="form.capitalCalType"
              dictionary="capital_cal_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否固定管理费费率')" prop="whetherFixed">
            <el-radio-group v-model="form.whetherFixed">
              <el-radio
                v-for="dict in whetherFixedOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherFixed === 'Y'" :span="12">
          <el-form-item :label="$t('管理费费率(%)')" prop="managementRate">
            <Ratio v-model="form.managementRate" :controls="false" :min="0" :max="100" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              maxlength="2000"
              show-word-limit
              :placeholder="$t('备注')"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.whetherFixed === 'N'">
        <div class="area-header">
          <span class="area-header-title">{{ $t('费率明细') }}</span>
        </div>
        <el-table :data="form.fundManagementRateDetailsList" border class="formTable">
          <el-table-column
            :label="$t('序号')"
            type="index"
            align="center"
            show-overflow-tooltip
            width="50"
          >
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('出资人名称')"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.investorName }}&nbsp;
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('合伙人类型')"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.partnerType }}&nbsp;
            </template>
          </el-table-column>
          <el-table-column :label="$t('费率')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('费率') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'fundManagementRateDetailsList.' + scope.$index + '.managementRate'"
                :rules="fundManagementRateDetailsRules.managementRate"
              >
                <Money v-model="scope.row.managementRate" :placeholder="$t('费率') " />
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="managementfee:rate" />
    </div>
  </div>
</template>

<script>
import {
  getFundManagementRateMain,
  initDetails,
  saveFundManagementRateMain,
  getByFundIdAndPhase, checkFundPhase
} from '@/api/fund/managementfee/rate/fundManagementRateMain'

export default {
  data() {
    return {
      // 表单参数
      form: {
        id: undefined,
        fundId: undefined,
        phase: undefined,
        year: undefined,
        accrualBase: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
        fundManagementRateDetailsList: []
      },
      // 遮罩层
      loading: false,
      whetherFixedOptions: [],
      // 表单校验
      rules: {
        phase: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }, { validator: this.checkFundId, trigger: 'change' }],
        whetherFixed: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        capitalCalType: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        managementRate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      fundManagementRateDetailsRules: {
        managementRate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      queryForm: {
        fundId: '',
        phase: ''
      }
    }
  },
  created() {
    this.getDicts('sys_yes_no').then(response => {
      this.whetherFixedOptions = response.data
    })
    this.getDicts('fund_phase').then(response => {
      this.phaseOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        getFundManagementRateMain(data.id).then(response => {
          this.form = response.data
          this.phaseChange()
        })
      } else {
        this.reset()
        this.$set(this.form, 'fundId', this.$route.query.id)
      }
    },
    // 校验表单中的期限是否已经存在，防止重复录入
    checkFundId(rule, value, callback) {
      const data = {
        fundId: this.$route.query.id,
        id: this.form.id,
        phase: value
      }
      checkFundPhase(data).then(response => {
        if (response.data.flag) {
          callback()
        } else {
          callback(new Error(response.data.message))
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        phase: undefined,
        whetherFixed: 'N',
        managementRate: undefined,
        capitalCalType: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
        fundManagementRateDetailsList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundManagementRateMain(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    phaseChange() {
      if (!this.form.id && this.form.phase !== '') {
        this.loading = true
        initDetails(this.form.fundId, this.form.phase).then(response => {
          this.form.fundManagementRateDetailsList = response.rows
          this.loading = false
        })
      }
      this.getPhaseData()
    },
    // 根据基金阶段，获取基金基本信息当前阶段的期限和计提基数
    getPhaseData() {
      if (this.form.phase !== '') {
        this.queryForm.phase = this.form.phase
        this.queryForm.fundId = this.form.fundId
        getByFundIdAndPhase(this.queryForm).then(res => {
          this.form.year = res.data.year
          this.form.accrualBase = res.data.accrualBase
        })
      }
    }
  }

}
</script>
